#include "showpic.h"
#include "ui_showpic.h"
#include <QMessageBox>
#include <QDesktopWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>

showPic::showPic(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::showPic)
{
    ui->setupUi(this);

    ui->showLabel->setStyleSheet("border:2px solid red;");
    ui->changLabel->setStyleSheet("border:2px solid blue;");
}

showPic::~showPic()
{
    delete ui;
}

void showPic::on_closeButton_clicked()
{
    this->close();
}
/**
 * @brief showPic::on_openButton_clicked
 * 功能:打开图片按钮槽函数
 *           调用OpenCV进行处理
 */
void showPic::on_openButton_clicked()
{
    QString filename;
    filename=QFileDialog::getOpenFileName(this,
                                        tr("选择图像"),
                                        "",
//                                        tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));//可打开的文件类型
                                            tr("Images (realCamer*.jpg)"));//可打开的文件类型
    if(!filename.isEmpty())
    {
        QPixmap *pixmap = new QPixmap(filename);
        pixmap->scaled(ui->showLabel->size(), Qt::KeepAspectRatio);
        ui->showLabel->setScaledContents(true);
        ui->showLabel->setPixmap(*pixmap);
    }
    //灰度处理
    Mat imag, result;

    imag = imread(filename.toStdString(),16);
    cout << "open file name " << filename.toStdString();
    result = imag.clone();
    threshold(imag, result, 100, 250, THRESH_OTSU);

    string str = filename.toStdString();
    int pos = str.find("realCamer");

   imwrite(str.substr(0,pos)+"GrayPic.jpg",result);

   String newpath = str.substr(0,pos) +"/GrayPic.jpg";
   QPixmap *pixmap = new QPixmap(QString::fromStdString(newpath));
   pixmap->scaled(ui->changLabel->size(), Qt::KeepAspectRatio);
   ui->changLabel->setScaledContents(true);
   ui->changLabel->setPixmap(*pixmap);
}
